﻿using RimWorld;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Verse;
using Verse.AI;
using Verse.Sound;

namespace Miao.Stand
{
    /// <summary>
    /// 卡死追踪热源
    /// </summary>
    public class JobDriver_TrackingHeat : JobDriver
    {
        public override bool TryMakePreToilReservations(bool errorOnFailed)
        {
            return true;
        }

        protected override IEnumerable<Toil> MakeNewToils()
        {
            yield return Toils_Goto.Goto(TargetIndex.A, PathEndMode.Touch);
            yield return Toils_General.Do(() =>
            {
                GenExplosion.NotifyNearbyPawnsOfDangerousExplosive(pawn, DamageDefOf.Bomb, null, pawn.GetComp<SheeHeartAttackCompToken>().owner);
            });
            yield return Toils_General.WaitWith(TargetIndex.B, 30, true).FailOn(DownOn);
            //通知炸弹最好
            yield return Toils_General.Do(
                () =>
                {
                    var comp = pawn.GetComp<SheeHeartAttackCompToken>();
                    comp.owner.standPower.CurLevel -= 0.1f;
                    GenExplosion.DoExplosion(pawn.PositionHeld, pawn.Map, comp.explosionRadius, DamageDefOf.Bomb, pawn, -1, -1, null, null, null, null, null, 0, 1, false, null, 0, 1, 0, false, null, new List<Thing> { pawn, comp.owner, comp.owner.owner });
                    if (TargetA.Pawn == null)
                    {
                        StandSoundDefOf.NotPawn.PlayOneShot(new TargetInfo(pawn.Position, base.Map));
                    }
                    else
                    {
                        StandSoundDefOf.LookMe.PlayOneShot(new TargetInfo(pawn.Position, base.Map));
                    }
                });
            yield return Toils_General.WaitWith(TargetIndex.B, 100);
        }
        bool DownOn()
        {
            Pawn p = TargetA.Pawn;
            if (p != null)
            {
                if (p.Downed)
                {
                    return true;
                }
            }
            return false;
        }

    }
}
